seajs.use([
   'bigfoot/common', 
   'bigfoot/dhtml/form/form', 'bigfoot/widget/upload/ui', 
   'bigfoot/widget/upload/ui', 'bigfoot/widget/dataview/crud'], function($, _form, uploader, uploadUtils, crud){

   $.get('/userinfo/BaseInfoController/showInfo2.wl', function(json){
       var url;
       if(json.total == 0){
           // 指定为 新增 接口
           url = '/userInfo/BaseInfoController/addEnterpriseInfo.wl';

       }else{
           // 指定为 修改 接口
           url = '/userInfo/BaseInfoController/modifyEnterpriseInfo.wl';
           
           // 数据绑定
           var dataBindingFn = crud.dataBinding.delegate(null, null, true).after(subValues).after(function(){
               // 修正剩余字数
               seajs.use('bigfoot/form/autoInit', function(_autoInit){
                   _autoInit.whenpresscheckCharLength(document.querySelector('form.ss'));
               });
           });
           dataBindingFn(json);
           
           // 加载 logo
           $.get('/photo/photoController/showPhoto.wl', function(json){
               if(json.root.length){
                   var logoImg = json.root[0].imgUrl;
                   document.querySelector('.perview img').src = serviceUrl + '/' + logoImg;
               }
           },{
               imgType : 1
           });
       }
   
       var form = Object.create(_form);
       form.action = url;
       form.formEl = document.querySelector('form.ss');
       form.beforSubmit = beforSubmit;
       form.onSubmitSuccuess = onSubmitSuccuess;
       form.init();
           
   });

   function beforSubmit(formEl, e, className, key){        
       className = className || '.服务内容';
       key = key || 'serviceContent';

       var values = [], el = formEl.$('input[name=' + key + ']')(true);

       formEl.$(className)()(function(inputEl){
           if(inputEl.checked)values.push(inputEl.value);
       });
       el.value = values.join(';');

       if(key == 'serviceContent')arguments.callee(formEl, null, '.服务类型', 'serviceType');

   }

   function onSubmitSuccuess(result){
       if(result.success){
           $.msg('修改成功！', function(){
              window.location.reload();
           });
       }else $.alert('修改失败！');
   }
   

   function subValues(data, property){
       property = property || 'serviceContent';

       if(data[property]){
           var arr = data[property].split(';');
           // debugger;
           for(var i = 0, j = arr.length; i < j; i++){
               var value = arr[i];
               $('*[type=checkbox][value=' + value + ']').checked = true;
           }
       }
       if(property == 'serviceContent'){
           arguments.callee(data, 'serviceType');
           }
   }
});